package com.school.platform.servlet;

import com.school.platform.utils.DBConnectionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/infoList")
public class InfoListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String keyword = request.getParameter("keyword");
        List<Info> infoList = new ArrayList<>();

        try (Connection connection = DBConnectionUtil.getConnection()) {
            // 初始 SQL 语句，确保最后有空格，方便拼接条件
            String sql = "SELECT i.id, i.title, i.content, u.username, i.create_time " +
                    "FROM information i " +
                    "JOIN users u ON i.user_id = u.id ";
            // 标记是否有搜索关键词
            boolean hasKeyword = keyword != null && !keyword.isEmpty();
            if (hasKeyword) {
                sql += "WHERE i.title LIKE ? OR i.content LIKE ? ";
            }
            sql += "ORDER BY i.create_time DESC";

            try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                if (hasKeyword) {
                    // 设置查询参数
                    preparedStatement.setString(1, "%" + keyword + "%");
                    preparedStatement.setString(2, "%" + keyword + "%");
                }
                ResultSet resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    Info info = new Info();
                    info.setId(resultSet.getInt("id"));
                    info.setTitle(resultSet.getString("title"));
                    info.setContent(resultSet.getString("content"));
                    info.setUsername(resultSet.getString("username"));
                    info.setCreateTime(resultSet.getTimestamp("create_time"));
                    infoList.add(info);
                }
            }
        } catch (SQLException e) {
            // 打印异常信息，方便调试
            System.err.println("数据库查询出错: " + e.getMessage());
            e.printStackTrace();
        }

        request.setAttribute("infoList", infoList);
        request.getRequestDispatcher("infoList.jsp").forward(request, response);
    }

    public static class Info {
        private int id;
        private String title;
        private String content;
        private String username;
        private java.sql.Timestamp createTime;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public java.sql.Timestamp getCreateTime() {
            return createTime;
        }

        public void setCreateTime(java.sql.Timestamp createTime) {
            this.createTime = createTime;
        }
    }
}